﻿using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Path = System.IO.Path;

namespace work4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadFolders();
        }

        private void LoadFolders()
        {
            var drives = DriveInfo.GetDrives();
            foreach (var drive in drives)
            {
                var item = new TreeViewItem
                {
                    Header = drive.Name,
                    Tag = drive.RootDirectory
                };
                item.Items.Add(null);
                item.Expanded += FolderExpanded;
                FolderTreeView.Items.Add(item);
            }
        }

        private void FolderExpanded(object sender, RoutedEventArgs e)
        {
            var item = (TreeViewItem)sender;
            if (item.Items.Count != 1 || item.Items[0] != null)
            {
                return;
            }

            item.Items.Clear();

            try
            {
                var directory = (DirectoryInfo)item.Tag;
                foreach (var subdirectory in directory.GetDirectories())
                {
                    var subitem = new TreeViewItem
                    {
                        Header = subdirectory.Name,
                        Tag = subdirectory
                    };
                    subitem.Items.Add(null);
                    subitem.Expanded += FolderExpanded;
                    item.Items.Add(subitem);
                }

                foreach (var file in directory.GetFiles())
                {
                    if (file.Extension == ".exe" || file.Extension == ".txt")
                    {
                        var subitem = new ListViewItem
                        {
                            Content = file,
                            Tag = file
                        };
                        FileListView.Items.Add(subitem);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        private void OpenMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var item = (ListViewItem)FileListView.SelectedItem;
            if (item != null && item.Tag is FileInfo fileInfo)
            {
                if (fileInfo.Extension == ".exe")
                {
                    Process.Start(fileInfo.FullName);
                }
                else if (fileInfo.Extension == ".txt")
                {
                    Process.Start("notepad.exe", fileInfo.FullName);
                }
            }
        }

        private void QuitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void AboutMenuItem_Click(object sender, RoutedEventArgs e)
        {
        }

        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            var item = (ListViewItem)FileListView.SelectedItem;
            if (item != null && item.Tag is FileInfo fileInfo)
            {
                if (fileInfo.Extension == ".exe")
                {
                    Process.Start(fileInfo.FullName);
                }
                else if (fileInfo.Extension == ".txt")
                {
                    Process.Start("notepad.exe", fileInfo.FullName);
                }
            }
        }
    }
}